package com.guochu.pro.controller;

import com.guochu.pro.entity.Chat;
import com.guochu.pro.service.ChatService;
import com.guochu.utils.MapUtils;
import com.guochu.utils.Pagination;
import io.micrometer.core.instrument.util.StringUtils;
import org.springframework.web.bind.annotation.*;
import com.guochu.common.ResultMap;

import javax.annotation.Resource;
import java.util.Arrays;
import java.util.List;
import java.util.Map;

/**
 * 产品分类表(Chat)表控制层
 *
 * @author makejava
 * @since 2021-07-05 23:05:08
 */
@RestController
@RequestMapping("pro/chat")
public class ChatController {
    /**
     * 服务对象(service层)
     */
    @Resource
    private ChatService chatService;

    /**
     * 添加单条记录
     *
     * @param chat 实例对象
     * @return
     */
    @RequestMapping(value = "/add", method = RequestMethod.POST)
    public ResultMap add(@RequestBody Chat chat) {
        int result = this.chatService.insert(chat);
        return ResultMap.success().put("id", chat.getId());
    }

    /**
     * 用户发送给客服
     *
     * @param chat
     * @return
     */
    @RequestMapping(value = "/sendToAdmin", method = RequestMethod.POST)
    public ResultMap sendToAdmin(@RequestBody Chat chat) {
        int result = this.chatService.sendToAdmin(chat);
        return ResultMap.success().put("id", chat.getId());
    }

    /**
     * 修改单条记录
     *
     * @param chat 实例对象
     * @return
     */
    @RequestMapping(value = "/update", method = RequestMethod.POST)
    public ResultMap update(@RequestBody Chat chat) {
        if (chat.getId() != null) {
            int result = this.chatService.update(chat);
            return ResultMap.success();
        } else {
            return ResultMap.success();
        }
    }

    /**
     * 逻辑删除多条记录
     *
     * <p>前端入参为 逗号相隔的ID字符串
     * 正例：id='ID,ID,ID';
     *
     * @param limit 主键id
     * @return
     */
    @RequestMapping(value = "/remove", method = RequestMethod.POST)
    public ResultMap remove(@RequestBody Map<String, Object> limit) {
        String ids = MapUtils.getString(limit, "id");
        List<String> idList = Arrays.asList(ids.split(","));
        int result = this.chatService.removeByIds(idList);
        return ResultMap.success();
    }

    /**
     * 通过主键ID查询单条数据
     *
     * @param limit 主键
     * @return 单条数据
     */
    @RequestMapping(value = "/getOne", method = RequestMethod.POST)
    public Chat getOne(@RequestBody Map<String, Object> limit) {
        String id = MapUtils.getString(limit, "id");
        return this.chatService.queryById(id);
    }

    /**
     * 根据条件查询分页数据
     *
     * @param limit 查询条件
     * @return 分页数据
     */
    @RequestMapping(value = "/selectPage", method = RequestMethod.POST)
    public ResultMap selectPage(@RequestBody Map<String, Object> limit) {
        //数据校验
        if (limit.get("currentPage") == null || limit.get("pageSize") == null || StringUtils.isBlank(limit.get("currentPage").toString()) || StringUtils.isBlank(limit.get("pageSize").toString())) {
            return ResultMap.error("");
        }
        // 定义分页参数
        Integer currentPage = MapUtils.getInteger(limit, "currentPage");
        Integer pageSize = MapUtils.getInteger(limit, "pageSize");
        Pagination pagination = new Pagination(currentPage, pageSize);
        //创建接收集合
        List<Chat> result = this.chatService.queryPageByLimit(pagination, limit);
        return ResultMap.success().put("items", result).put("page", pagination);
    }

    /**
     * 用户小程序端获取聊天信息
     *
     * @param limit
     * @return
     */
    @RequestMapping(value = "/selectPageByObject", method = RequestMethod.POST)
    public ResultMap selectPageByObject(@RequestBody Map<String, Object> limit) {
        //数据校验
        if (limit.get("currentPage") == null || limit.get("pageSize") == null || StringUtils.isBlank(limit.get("currentPage").toString()) || StringUtils.isBlank(limit.get("pageSize").toString())) {
            return ResultMap.error("");
        }
        // 定义分页参数
        Integer currentPage = MapUtils.getInteger(limit, "currentPage");
        Integer pageSize = MapUtils.getInteger(limit, "pageSize");
        Pagination pagination = new Pagination(currentPage, pageSize);
        //创建接收集合
        List<Chat> result = this.chatService.selectPageByObject(pagination, limit);
        return ResultMap.success().put("items", result).put("page", pagination);
    }

    /**
     * 根据条件查询全部数据
     *
     * @param limit 查询条件
     * @return 所有数据
     */
    @RequestMapping(value = "/selectAll", method = RequestMethod.POST)
    public ResultMap selectAll(@RequestBody Map<String, Object> limit) {
        List<Chat> result = this.chatService.queryAllByLimit(limit);
        return ResultMap.success().put("items", result);
    }

    /**
     * 客服获取聊天信息
     *
     * @param limit
     * @return
     */
    @RequestMapping(value = "/adminGetAll", method = RequestMethod.POST)
    public ResultMap adminGetAll(@RequestBody Map<String, Object> limit) {
        List<Chat> result = this.chatService.adminGetAll(limit);
        return ResultMap.success().put("items", result);
    }
}
